var sexChange = document.getElementById("sexChange");
var sexText = document.getElementById("sexText");
var bothDate = document.getElementById("bothDate");
var token;

(function($) {

	setPxPerRem();

	mui.plusReady(function() {

		token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
		if(isHaveNet()) {
			getUserInfo(token);
		} else {
			mui.toast("网络连接失败，请稍后重试")
		}
	});

})(mui);
var sexStr = "";
sexChange.addEventListener('click', function() {
	var btnArray = [{
		title: "男"
	}, {
		title: "女"
	}, {
		title: "保密"
	}];
	plus.nativeUI.actionSheet({
		title: "选择性别",
		cancel: "取消",
		buttons: btnArray
	}, function(e) {
		var index = e.index;
		switch(index) {
			case 0:
				sexStr = "取消";
				break;
			case 1:
				sexStr = "男";
				break;
			case 2:
				sexStr = "女 ";
				break;
			case 3:
				sexStr = "保密";
				break;
		}
		sexText.innerHTML = sexStr;
		if(index == 3) {
			index = 0;
		}
		var postJson = {
			sex: index + ""
		}
		changeInfo(postJson, token);
	});
}, false);

var dtpicker = new mui.DtPicker({
	type: "date", //设置日历初始视图模式 
	beginDate: new Date(1950, 01, 01), //设置开始日期 
	endDate: new Date(), //设置结束日期 
	labels: ['年', '月', '日'], //设置默认标签区域提示语 
	customData: {}
})

bothDate.addEventListener('click', function() {
	dtpicker.show(function(e) {
		document.getElementById("dateDiv").innerHTML = e;
		var postJson = {
			birthday: e + ""
		}
		changeInfo(postJson, token);
	})
}, false);
//page_changeinfo.html

$(document).ready(function() {
	$("#nickName").click(function() {
		var content = $("#nickname").html();
		changePageInfo(content, "修改昵称", 1);
	});

	$("#userName").click(function() {
		mui.toast("用户名不可修改！");
	});

	$("#phone").click(function() {
		var content = $("#phoneNum").html();
		changePageInfo(content, "修改手机号", 2);
	});

	$("#idCardDiv").click(function() {
		mui.toast("暂不支持绑定身份证号");
//		var content = $("#idCard").html();
//		changePageInfo(content, "绑定身份证号", 3);
	});

});

window.addEventListener("backResult", function(event) {
	console.log("changeStr==>>" + "5555555");
	var flag = event.detail.flag;
	var changeStr = event.detail.changeStr;
	console.log("changeStr==>>" + changeStr);
	var postJson;
	switch(flag) {
		case 1:
			$("#nickname").html(changeStr);
			postJson = {
				nickname: changeStr
			}
			changeInfo(postJson, token);
		case 2:
			$("#phoneNum").html(changeStr);
			postJson = {
				mobile: changeStr
			}
			break;
//		case 3:
//			$("#idCardDiv").html(changeStr);
//			postJson = {
//				birthday: e + ""
//			}
			break;
		default:
			mui.toast("修改信息失败");
			return;
			break;
	}
	changeInfo(postJson, token);

});

function changePageInfo(postContent, title, flag) {
	var postData = {
		pageflag: "page_meinfo.html",
		postContent: postContent,
		title: title,
		flag: flag
	}
	var login = {
		pageUrl: "page_changeinfo.html",
		postData: postData,
		aniShow: "fade-in"
	};
	pageChange(login);
}

function getUserInfo(authorization) {
	$.ajax({
		type: "GET",
		url: getRequestAddressUrl("testline") + '/user/profile',
		headers: {
			"accept": "application/json",
			"authorization": authorization
		}
	}).done(function(data) {
		if(!isEmpty(data)) {
			logText("data==>>" + JSON.stringify(data));
			var row = data.data;
			if(!isEmpty(row)) {
				var userInfoStr = JSON.stringify(row);
				localStorage.setItem("userInfo", userInfoStr);
				if(!isEmpty(row.username)) {
					$("#name").html(row.username);
				}
				if(!isEmpty(row.nickname)) {
					$("#nickname").html(row.nickname);
				}
				if(!isEmpty(row.sex)) {
					var sexStr = row.sex == 0 ? "未知" : (row.sex == 1 ? "男" : "女");
					$("#sexText").html(sexStr);
				} else {
					$("#sexText").html("未知");
				}
				if(isEmpty(row.mobile)) {
					row.mobile = '';
				}
				$("#phoneNum").html(row.mobile);

				if(!isEmpty(row.id_card)) {
					$("#idCard").html(row.id_card);
				} else {
					$("#idCard").html("未绑定");
				}
				if(!isEmpty(row.birthday)) {
					$("#dateDiv").html((row.birthday.substr(0 , 10)));
				} else {
					$("#dateDiv").html("未设置");
				}
			}
		}
	});
}

function changeInfo(postData, keyStr) {
	if(checkParams(postData)) {
		mui.toast("请重新选择修改信息");
		return;
	}
	var settings = {
		url: getRequestAddressUrl("testline") + "/user/profile",
		method: "POST",
		data: postData,
		headers: {
			"accept": "application/json",
			"authorization": keyStr
		}
	}
	plus.nativeUI.showWaiting();
	$.ajax(settings).done(function(data) {
		plus.nativeUI.closeWaiting();
		if(isEmpty(data)) {
			mui.toast("修改失败，请稍候再试");
		}
		var meta = data.meta;
		toastMesg(meta, 10000, 20000);
		if(!isEmpty(meta)) {
			if(!isEmpty(meta.code) && meta.code == 10000) {
				mui.toast("修改成功");
			} else {
				mui.toast("修改失败");
			}
		}
	});

}